﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

using Infrastructure.Utility;
using Infrastructure.Core.IOC;
using SMG.ServiceContract.System;
using SMG.Monitor.Domain.Services;

namespace SMG.Monitor.WinService
{
    /// <summary>
    /// 节目单监控服务
    /// </summary>
    partial class ProgramMonitorService : ServiceBase
    {
        ICommonService commonService = ServiceLocator.Instance.Resolve<ICommonService>();
        IMonitor programMonitor = null;

        public ProgramMonitorService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            try
            {
                MonitorSetting setting = null;
                var dict = commonService.GetDictByKey("ProgramMonitorSetting");
                if (dict != null) {
                    setting = SerializeUtility.JsonDeserialize<MonitorSetting>(dict.DictValue);
                }
                if (setting == null) setting = new MonitorSetting();
                programMonitor = new ProgramService(setting);
                programMonitor.Start();
            }
            catch (Exception ex) { 
                
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            try
            {
                if (programMonitor != null)
                    programMonitor.Stop();
            }
            catch (Exception ex) { 
                
            }
        }
    }
}
